package com.ant.cn.duomen01;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import cn.dow.android.DOW;
import cn.dow.android.listener.DLoadListener;
import cn.dow.android.listener.DataListener;


public class MainActivity extends Activity {

	private TextView showPointTV;
	private EditText pointEt;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.dm_layout_main);
		//userid不能写死，可以为空值
		DOW.getInstance(this).init(new DLoadListener() {
			
			@Override
			public void onSuccess() {
				// TODO Auto-generated method stub
				Log.e("---->----","成功");
			}
			
			@Override
			public void onStart() {
				// TODO Auto-generated method stub
				Log.e("---->----","加载开始");
			}
			
			@Override
			public void onLoading() {
				// TODO Auto-generated method stub
				Log.e("---->----","加载中");
			}
			
			@Override
			public void onFail() {
				// TODO Auto-generated method stub
				Log.e("---->----","失败");
			}
		});
		showPointTV = (TextView) findViewById(R.id.showPoints);
		pointEt = (EditText) findViewById(R.id.consumePoint);
	}

	public void click(View v) {
		int viewId = v.getId();
		switch (viewId) {
		case R.id.getAdList: // 获取任务列表
			Intent intent = new Intent(this, ShowOfferListActivity.class);
			intent.putExtra("LOADINGFLAG", 0);
			this.startActivity(intent);
			break;
		case R.id.getReopenAdList: // 获取签到任务列表
			Intent intent2 = new Intent(this, ShowOfferListActivity.class);
			intent2.putExtra("LOADINGFLAG", 1);
			this.startActivity(intent2);
			break;
		case R.id.openSDK:
			DOW.getInstance(this).show(this);
			break;
		case R.id.checkPoints: // 查询积分
			checkPoints();
			break;
		case R.id.consumePoints: // 消费积分
			consumePoints();
			break;
		case R.id.getUnitName://获取货币单位
			String unitName=DOW.getInstance(this).getUnitName();
			Toast.makeText(this, "积分单位为："+unitName, Toast.LENGTH_SHORT).show();
			break;
		}
	}

	/**
	 * 查询积分
	 */
	private void checkPoints() {
		DOW.getInstance(this).checkPoints(new DataListener() {

			@Override
			public void onError(String arg0) {
			}

			@Override
			public void onResponse(Object... arg0) {
				showText("已消费:" + arg0[0] + "\n总积分:" + arg0[1]);
			}
		});
	}

	private void consumePoints() {
		int consumePoint;
		String pointStr = pointEt.getText().toString();
		if (TextUtils.isEmpty(pointStr)) {
			Toast.makeText(this, "请输入您要消费的积分!!", Toast.LENGTH_SHORT).show();
			return;
		} else {
			try {
				consumePoint = Integer.parseInt(pointStr);
			} catch (Exception e) {
				Toast.makeText(this, "请输入整数类型的数字", Toast.LENGTH_SHORT).show();
				return;
			}
		}

		DOW.getInstance(this).consumePoints(consumePoint, new DataListener() {

			@Override
			public void onResponse(Object... objects) {
				switch ((Integer) objects[0]) {
				case 1:
					Toast.makeText(MainActivity.this, "消费成功",
							Toast.LENGTH_SHORT).show();
					break;
				case 2:
					Toast.makeText(MainActivity.this, "积分不足",
							Toast.LENGTH_SHORT).show();
					break;
				case 3:
					Toast.makeText(MainActivity.this, "重复订单",
							Toast.LENGTH_SHORT).show();
					break;
				case 4:
					Toast.makeText(MainActivity.this, "未知错误",
							Toast.LENGTH_SHORT).show();
					break;

				default:
					break;
				}
				//showText("已消费:" + objects.length);
				showText("已消费:" + objects[1] + "\n总积分:" + objects[2]);
			}

			@Override
			public void onError(String errorInfo) {

			}
		});
	}

	private void showText(String text) {
		showPointTV.setText(text);
	}

}
